import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const router = new Router({
  routes: [
    { path: '/', redirect: '/login' },
    { path: '/login', name: 'login', component: () => import('@/pages/Login/Login') },
    {
      path: '/home',
      redirect: '/welcome',
      name: 'Home',
      component: () => import(/* webpackChunkName: "Home" */'./pages/Home/Home.vue'),
      children: [
        { path: '/welcome', name: 'welcome', component: () => import(/* webpackChunkName: "Home" */'./pages/Home/Welcome.vue') },
        { path: '/users', name: 'users', component: () => import(/* webpackChunkName: "User" */'./pages/Users/Users.vue') },
        { path: '/rights', name: 'rights', component: () => import(/* webpackChunkName: "Power" */'./pages/Power/Rights.vue') },
        { path: '/roles', name: 'roles', component: () => import(/* webpackChunkName: "Power" */'./pages/Power/Roles.vue') },
        { path: '/categories', name: 'categories', component: () => import(/* webpackChunkName: "Goods" */'./pages/Goods/Categories.vue') },
        { path: '/goods', name: 'goods', component: () => import(/* webpackChunkName: "Goods" */'./pages/Goods/Goods.vue') },
        { path: '/params', name: 'params', component: () => import(/* webpackChunkName: "Goods" */'./pages/Goods/Params.vue') },
        { path: '/goods/add', name: 'add', component: () => import(/* webpackChunkName: "Goods" */'./pages/Goods/Add.vue') },
        { path: '/orders', name: 'orders', component: () => import(/* webpackChunkName: "Orders" */'./pages/Orders/Orders.vue') },
        { path: '/reports', name: 'reports', component: () => import(/* webpackChunkName: "Reports" */'./pages/Reports/Reports.vue') }
      ]
    }
  ]
})
// 验证token
router.beforeEach((to, from, next) => {
  if (to.path === '/login') { return next() }
  const token = window.sessionStorage.getItem('token')
  if (!token) { return next('/login') }
  next()
})
export default router
